<template>
	<view>
		<view class="z-flex">
			<image :src="cdn(config.skill_image)" class="logo_img" mode="" />
		</view>
		<view class="z-flex">
			<view class="z-font-w title">
				{{ config.skill_name }}
			</view>
		</view>
		<view class="z-flex z-margin-t-32 text_999 z-font-28">{{ config.skill_intro }}</view>

		<view class="z-flex btn-box" style="margin-top: 200rpx">
			
			
			<button open-type="getPhoneNumber" class="btn theme-colors" @getphonenumber="getPhoneNumber">手机号快捷登录</button>
			<!-- <button class="btn theme-colors" @tap="getPhoneNumber">手机号快捷登录</button> -->
			
			
			<view class="mask z-margin-l-32" @tap="read" v-if="!agree"></view>
		</view>
		<view class="z-flex" @tap="clickread">
			<image src="../../static/service/check.png" v-if="!agree" class="choose_img" mode="" />
			<image src="../../static/service/checkbox.png" v-else class="choose_img" mode="" />
			<view class="text_999 z-font-28 z-margin-l-8">
				已阅读并同意
				<text class="money" @tap.stop="infoTap('用户协议')">《用户协议》</text>
				和
				<text class="money" @tap.stop="infoTap('隐私政策')">《隐私政策》</text>
			</view>
		</view>
		<u-popup :show="show" mode="bottom" @close="onClose">
			<view class="z-font-30 z-font-w z-margin-32">完善个人信息</view>
			<view class="info-box z-margin-lr-32 z-padding-lr-32 z-padding-tb-8 z-radius-20">
				<view class="avatar-box z-margin-tb-24 z-padding-tb-12 z-flex-c">
					<view class="z-font-22 text_666 z-margin-r-32">头像</view>
					<image class="avatar z-radius" :src="cdn(avatarUrl)"></image>
					<view class="z-flex-1"></view>
					<!-- <van-icon name="arrow" /> -->
					<button class="z-absolute" open-type="chooseAvatar" @chooseavatar="chooseAvatar"></button>
				</view>
				<view class="avatar-box z-margin-tb-24 z-flex-c">
					<view class="z-font-22 text_666 z-margin-r-32">昵称</view>
					<input type="nickname" class="z-flex-1 z-font-28" v-model="nickname" placeholder="请输入昵称" @nicknamereview="namereview" />
				</view>
			</view>
			<view class="z-padding-32">
				<view class="z-btn" @tap="saveInfo">保存</view>
			</view>
		</u-popup>

		<!-- 弹出层 -->
		<u-popup :show="refuse" mode="center" @close="closeRefuse">
			<view class="popup-box">
				<view class="data-img-box z-margin-t-40">
					<image class="data-img" src="../../static/service/data-img.png"></image>
				</view>
				<view class="z-font-30 z-text-c z-margin-t-8">申请入驻失败</view>
				<view class="z-font-28 z-padding-t-32 z-padding-lr-60 content">拒绝原因：{{ note }}</view>

				<view class="z-radius-20 z-text-c refer-btn" @tap="resubmit">重新提交</view>
			</view>
		</u-popup>
		<u-popup :round="10" mode="center" :show="waitSHow" @close="waitClose">
			<view class="popup-main">
				<text class="z-text-c pop-text z-margin-t-48">感谢您对我们平台工作的支持，您的申请正在审核中</text>
				<view class="z-text-c z-margin-b-40 pop-btn z-margin-t-48" @tap="waitClose">好的</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
export default {
	data() {
		return {
			agree: false,
			code: '',
			avatarUrl: '',
			nickname: '',
			show: false,
			safeBottom: `30px`,
			refuse: false,
			note: '',
			waitSHow: false,
			token: '',
			config: uni.getStorageSync('config'),
			userInfo: ''
		};
	},
	onLoad(options) {
		this.getCode();
	},
	methods: {
		chooseAvatar(e) {
			const { avatarUrl } = e.detail;

			this.http.upload(
				avatarUrl,
				{
					type: 'image'
				},
				false,
				true,
				this.token
			).then((res) => {
				this.avatarUrl = res.data.url
			});
		},
		saveInfo() {
			this.http.post(
				'index/updateuserinfo',
				{
					type: 'update',
					avatar: this.avatarUrl,
					nickname: this.nickname
				},
				true,
				false,
				this.token
			).then((res) => {
				this.show = false
				this.util.toast(res.msg);
				if (this.userInfo.is_skill === 0) {
					this.getResult();
				} else if (this.userInfo.is_skill === 1) {
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}
			});
		},
		nameInput(e) {
			this.nickname = e.detail.value
		},
		infoTap(name) {
			if(name == '用户协议'){
				let id = uni.getStorageSync('config').service_agreement_info_id
				this.util.skip('/service/info/info?id=' + id + '&name=' + name);
			} else {
				let id = uni.getStorageSync('config').service_privacy_info_id
				this.util.skip('/service/info/info?id=' + id + '&name=' + name);
			}
		},
		namereview(e) {},
		waitClose() {
			this.util.back();
		},
		closeRefuse() {
			this.refuse = false
		},
		onClose() {
			this.show = false
			if (this.userInfo.is_skill === 0) {
				this.getResult();
			} else if (this.userInfo.is_skill === 1) {
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}
		},
		getCode() {
			let that = this;
			uni.login({
				success(res) {
					if (res.code) {
						that.code = res.code
					} else {
						this.getCode();
					}
				}
			});
		},

		read() {
			if (!this.agree) {
				uni.showToast({
					title: '请先阅读并同意《用户协议》《隐私政策》',
					icon: 'none'
				});
			}
		},
		resubmit() {
			this.util.skip('/service/enter/enter?state=-1&token=' + this.token);
		},
		getResult() {
			this.http.post('skill/apply', {}, true, false, this.token).then((res) => {
				if (res.data) {
					if (res.data.state == -1) {
						this.refuse = true,
						this.note = res.data.note
					} else if (res.data.state == 0) {
						this.waitSHow = true
					} else if (res.data.state == 1) {
						uni.reLaunch({
							url: '/service/enter/enter?token=' + this.token
						});
					}
				} else {
					this.util.skip('/service/enter/enter?token=' + this.token);
				}
			});
		},
		getPhoneNumber(e) {
			// uni.reLaunch({
			// 	url: '/service/enter/enter?token=6d47a971-3284-4c63-8337-fac9e48b04a6'
			// });
			// return
			this.http.post(
				'user/userlogin',
				{
					code: this.code,
					type: 1,
					encryptedData: e.detail.encryptedData,
					iv: encodeURIComponent(e.detail.iv)
				},
				true,
				false
			)
				.then((res) => {
					this.getCode();
					this.userInfo = res.data.userInfo

					if (res.data.userInfo.is_skill === 1) {
						uni.setStorageSync('token', res.data.user.token);
						uni.setStorageSync('user', res.data.user);
						uni.setStorageSync('userInfo', res.data.userInfo);
						// app.globalData.token = res.data.user.token;
						// app.globalData.user = res.data.user;
						// app.globalData.userInfo = res.data.userInfo;
					} else {
						this.token = res.data.user.token
					}
					if (res.data.userInfo.is_update === 0) {
						this.show = true,
						this.avatarUrl = res.data.user.avatar,
						this.nickname = res.data.user.nickname
					} else if (res.data.userInfo.is_skill === 0) {
						this.getResult();
					} else {
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}
				})
				.catch((err) => {
					console.log(err);
					this.getCode();
				});
		},

		clickread() {
			this.agree = !this.agree
		}
	}
};
</script>

<style>
/* service/login/loign.wxss */
page {
	background-color: #fff;
	position: relative;
}
.logo_img {
	margin-top: 100rpx;
	width: 215rpx;
	height: 215rpx;
}
.title {
	font-size: 50rpx;
	margin-top: 200rpx;
}
.money {
	color: #ff9600;
}
.choose_img {
	width: 30rpx;
	height: 30rpx;
}
.btn-box {
	position: relative;
}
.btn {
	width: 686rpx;
	height: 98rpx;
	box-sizing: border-box;
	border: none;
	color: #fff;
	padding: 30rpx;
	border-radius: 16rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-bottom: 76rpx;
	font-size: 30rpx;
}
.mask {
	position: absolute;
	left: 0;
	top: 0;
	width: 686rpx;
	height: 98rpx;
}

.info-box {
	background-color: #f5f7f9;
}

.avatar-box {
	position: relative;
}

.avatar {
	width: 68rpx;
	height: 68rpx;
}

.popup-box {
	width: 560rpx;
	height: 628rpx;
	background: #ffffff;
	border-radius: 30rpx 30rpx 30rpx 30rpx;
	opacity: 1;
	position: relative;
}
.data-img-box {
	width: 178rpx;
	height: 178rpx;
	margin: auto;
}
.data-img {
	width: 178rpx;
	height: 178rpx;
}
.content {
	line-height: 46rpx;
	color: #6b738b;
	height: 200rpx;
	overflow: hidden;
	overflow-y: auto;
}
.refer-btn {
	width: 496rpx;
	height: 100rpx;
	background: #1677ff;
	line-height: 100rpx;
	color: #fff;
	position: absolute;
	bottom: 10rpx;
	left: 50%;
	transform: translate(-50%, -50%);
}

.popup-main {
	width: 560rpx;
	/* height: 372rpx; */
	background: #ffffff;
	border-radius: 30rpx 30rpx 30rpx 30rpx;
	opacity: 1;
}
.pop-text {
	display: block;
	width: 444rpx;
	margin: auto;
	line-height: 46rpx;
}
.pop-btn {
	width: 496rpx;
	height: 100rpx;
	background: #1677ff;
	border-radius: 20rpx 20rpx 20rpx 20rpx;
	opacity: 1;
	margin: auto;
	line-height: 100rpx;
	color: #fff;
}
</style>
